package com.x.client.activity

import android.support.v7.widget.GridLayoutManager
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.chad.library.adapter.base.BaseQuickAdapter
import com.gg.baselibrary.BaseActivity
import com.gg.utils.StatusUtils
import com.gg.widget.navigationbar.DefaultNavigationBar
import com.x.client.R
import com.x.client.adapter.PriceAdapter
import com.x.client.bean.CarStateBus
import com.x.client.bean.PriceItem
import com.x.client.utils.RouterClass
import com.x.client.utils.RouterField
import kotlinx.android.synthetic.main.activity_car_subscribe.*

/**
 * Creator : GG
 * Date    : 2018/6/8
 * Mail    : gg.jin.yu@gmai.com
 * Explain :
 */
@Route(path = RouterClass.carSubscribe)
class CarSubscribeActivity :BaseActivity() {


    @Autowired(name = RouterField.data)
    lateinit var bus: CarStateBus

    private var priceStr = ""

    private val ageArray: Array<String> by lazy { resources.getStringArray(R.array.car_age_array) }

    private val mileageArray: Array<String> by lazy { resources.getStringArray(R.array.mileage_array) }

    private val priceAdapter: PriceAdapter by lazy {
        PriceAdapter(data = listOf(PriceItem(name = "不限"), PriceItem(name = "0~3万"), PriceItem(name = "3~5万"), PriceItem(name = "5~10万"),
                PriceItem(name = "10~15万"), PriceItem(name = "15~20万"), PriceItem(name = "20~30万"), PriceItem(name = "30~50万"), PriceItem(name = "50万以上")))
    }

    override fun initArguments() {
        super.initArguments()
        if (!this::bus.isInitialized)
            bus = CarStateBus()
    }

    override fun getLayoutRes(): Int = R .layout.activity_car_subscribe

    override fun initTitle() {
        super.initTitle()
        StatusUtils.setStatusTextColor(true, this)
        StatusUtils.setStatusBar(this, false, false)
        DefaultNavigationBar.Builder(this)
                .setMiddleText(getString(R.string.car_subscribe))
                .setStatusBarHeight(StatusUtils.getStatusBarHeight(this))
                .setLeftIcon(R.drawable.ic_back_black)
                .setLeftClickListener(View.OnClickListener { finish() })
                .create()
    }

    override fun initView() {

        initPrice()

        initCarAge()

        initMileage()

        initBox()
    }

    private fun initBox() {

    }

    private fun initMileage() {
        mileageRangeBar.setOnRangeSelectedListener { left, right ->
            setMileage(left,right)
        }
        setMileage(bus.fromMileageIndex?:0,bus.toMileageIndex?:5)
    }

    private fun initCarAge() {
        ageRangeBar.setOnRangeSelectedListener { left, right ->
            setCarAge(left, right)
        }
        setCarAge(bus.fromCarAgeIndex?:0, bus.toCarAgeIndex?:7)
    }

    private fun setCarAge(left: Int, right: Int) {
        if (right == ageArray.size - 1 && left == 0) {
            carAge.text = ageArray[right]
            bus.fromCarAge = null
            bus.toCarAge = null
        } else if (right == ageArray.size - 1 && left != 0) {
            carAge.text = ageArray[left] + "年以上"
            bus.fromCarAge = ageArray[left].toInt()
            bus.toCarAge = null
        } else if (right != ageArray.size - 1 && left != 0) {
            carAge.text = ageArray[left] + "-" + ageArray[right] + "年"
            bus.fromCarAge = ageArray[left].toInt()
            bus.toCarAge = ageArray[right].toInt()
        } else if (right != ageArray.size - 1 && left == 0) {
            carAge.text = ageArray[right] + "年以内"
            bus.fromCarAge = null
            bus.toCarAge = ageArray[right].toInt()
        }
        bus.fromCarAgeIndex = left
        bus.toCarAgeIndex = right
    }

    private fun setMileage(left: Int, right: Int) {
        if (right == mileageArray.size - 1 && left == 0) {
            mileage.text = mileageArray[right]
            bus.fromMileage = null
            bus.toMileage = null
        } else if (right == mileageArray.size - 1 && left != 0) {
            mileage.text = mileageArray[left] + "万公里以上"
            bus.fromMileage = mileageArray[left].toInt()
            bus.toMileage = null
        } else if (right != mileageArray.size - 1 && left != 0) {
            mileage.text = mileageArray[left] + "-" + mileageArray[right] + "万公里"
            bus.fromMileage = mileageArray[left].toInt()
            bus.toMileage = mileageArray[right].toInt()
        } else if (right != mileageArray.size - 1 && left == 0) {
            mileage.text = mileageArray[right] + "万公里以内"
            bus.fromMileage = null
            bus.toMileage = mileageArray[right].toInt()
        }
        bus.fromMileageIndex = left
        bus.toMileageIndex = right
    }

    /**
     * 价格筛选的设置
     */
    private fun initPrice() {
        priceRecyclerView.layoutManager = GridLayoutManager(this, 3)
        priceRecyclerView.adapter = priceAdapter

        priceAdapter.onItemClickListener = BaseQuickAdapter.OnItemClickListener { _, _, position ->
            if (bus.pricePosition != -1) {
                priceAdapter.data[bus.pricePosition ?: 0].status = false
                priceAdapter.notifyItemChanged(bus.pricePosition ?: 0)
            }
            when (position) {
                0 -> {
                    bus.fromRetailPrice = null
                    bus.toRetailPrice = null
                }
                1 -> {
                    bus.fromRetailPrice = 0.0
                    bus.toRetailPrice = 3.0
                }
                2 -> {
                    bus.fromRetailPrice = 3.0
                    bus.toRetailPrice = 5.0
                }
                3 -> {
                    bus.fromRetailPrice = 5.0
                    bus.toRetailPrice = 10.0
                }
                4 -> {
                    bus.fromRetailPrice = 10.0
                    bus.toRetailPrice = 15.0
                }
                5 -> {
                    bus.fromRetailPrice = 15.0
                    bus.toRetailPrice = 20.0
                }
                6 -> {
                    bus.fromRetailPrice = 20.0
                    bus.toRetailPrice = 30.0
                }
                7 -> {
                    bus.fromRetailPrice = 30.0
                    bus.toRetailPrice = 50.0
                }
                8 -> {
                    bus.fromRetailPrice = 50.0
                    bus.toRetailPrice = null
                }
            }
            bus.pricePosition = position
            if (bus.pricePosition != -1) {
                priceAdapter.data[bus.pricePosition ?: 0].status = true
                priceAdapter.notifyItemChanged(bus.pricePosition ?: 0)
            }
            setPrice()
        }
        setPrice()
    }

    private fun setPrice() {
        priceStr =
                if (bus.fromRetailPrice != null) {
                    if (bus.toRetailPrice != null)
                        "${bus.fromRetailPrice}-${bus.toRetailPrice}万"
                    else
                        "${bus.fromRetailPrice}万以上"
                } else {
                    if (bus.toRetailPrice != null)
                        "${bus.toRetailPrice}万以内"
                    else
                        "不限"
                }

        price.text = priceStr
    }

    override fun initData() {
    }
}